package com.gree.changying.activity.mainactivity.framgent.homefrm;


import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.gree.changying.R;
import com.gree.changying.activity.base.BasePageFragment;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.adapter.GdhlAdapter;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.adapter.RmmjAdapter;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.adapter.RmssAdapter;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.adapter.ViewPagerAdapter;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.adapter.ZxzxAdapter;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.bean.Gdhl;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.bean.Rmmj;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.bean.Rmss;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.bean.Zxzx;
import com.gree.changying.utils.DisplayUtil;
import com.gree.changying.view.AutoRecyclerView;
import com.gree.changying.view.AutoViewPager;
import com.gree.changying.widget.banner.BannerLayout;
import com.gree.changying.widget.banner.adapter.BannerAdapter;
import com.gree.changying.widget.banner.bean.Photo;
import com.gree.changying.widget.tablayout.jtablayout.tab.Tab;
import com.gree.changying.widget.tablayout.widget.JTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TuijianFrm extends BasePageFragment {
    @Override
    protected void initView(View view) {
        ButterKnife.bind(this,view);
        init();
        initBanner();
        initRmmj();
        initRmss();
        initZxzx();
        initHaoliao();
    }

    @Bind(R.id.topbanner)
    LinearLayout topbanner;
    @Bind(R.id.banner)
    BannerLayout banner;
    private void initBanner(){
        List<Photo> list = new ArrayList<>();
        Photo photo = new Photo();
        photo.setPath("-");
        photo.setBg(R.drawable.icon_haibao);
        list.add(photo);

        Photo photo1 = new Photo();
        photo1.setPath("-");
        photo1.setBg(R.drawable.icon_haibao);
        list.add(photo1);

        Photo photo2 = new Photo();
        photo2.setPath("-");
        photo2.setBg(R.drawable.icon_haibao);
        list.add(photo2);

        Display display = getActivity().getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        BannerAdapter bannerAdapter=new BannerAdapter(getContext(),list,width);

        int dp =DisplayUtil.dip2px(mContext,16f);
        int mwidth = width-dp*2;
        banner.setItemSpace(dp);
        int height = 534*mwidth/1331;
        RelativeLayout.LayoutParams layoutParams = ( RelativeLayout.LayoutParams)topbanner.getLayoutParams();
        layoutParams.height = height/3;
        layoutParams.setMargins(0,height-(height/3)+dp,0,0);
        topbanner.setLayoutParams(layoutParams);
        bannerAdapter.setOnBannerItemClickListener(position -> {
            // startActivity(MessageActivity.class);
        });
        banner.setAdapter(bannerAdapter);
        bannerAdapter.notifyDataSetChanged();
    }


    /**
     * 热门名家
     */
    @Bind(R.id.rmmjTab)
    JTabLayout rmmjTab;
    @Bind(R.id.rmmjPager)
    AutoViewPager rmmjPager;
    ViewPagerAdapter rmmjAdapter;
    private void initRmmj(){
        rmmjAdapter = new ViewPagerAdapter();
        rmmjPager.setAdapter(rmmjAdapter);
        rmmjPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rmmjTab.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        rmmjTab.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(@NonNull Tab var1) {
                rmmjPager.setCurrentItem(var1.getPosition());
            }

            @Override
            public void onTabUnselected(@NonNull Tab var1) {

            }

            @Override
            public void onTabReselected(@NonNull Tab var1) {

            }
        });
        initRmmjData();
    }
    private String[] rmmj = {"足球","篮球"};
    private Object[][] rmmjList = {
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.touxiang,"天降横财",43},
            {R.drawable.mingjia,"更多名家",0}
    };
    private List<Rmmj> getRmmj(){
        List<Rmmj> rmsses = new ArrayList<>();
        for(Object[] o:rmmjList){
            Rmmj rmss = new Rmmj();
            rmss.setImg((int)o[0]);
            rmss.setName((String)o[1]);
            rmss.setSl((int)o[2]);
            rmsses.add(rmss);
        }
        return rmsses;
    }
    private void initRmmjData(){
        List<View> views = new ArrayList<>();
        for(String s:rmmj){
            Tab tab = rmmjTab.newTab().setTitle(s);
            tab.setTitleColor(getResources().getColor(R.color.tabDefaultColor)
                    ,getResources().getColor(R.color.white));
            rmmjTab.addTab(tab);
            View tView =  LayoutInflater.from(mContext).inflate(R.layout.pager_recycleview, null);
            RecyclerView rv = tView.findViewById(R.id.rv);
            rv.setLayoutManager(new GridLayoutManager(mContext,4,LinearLayoutManager.VERTICAL,false){
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            rv.setHasFixedSize(true);
            RmmjAdapter rmmjAdapter = new RmmjAdapter(R.layout.item_tuijian_rmmj,getRmmj());
            rv.setAdapter(rmmjAdapter);
            views.add(tView);
        }
        rmmjAdapter.update(views);
        rmmjPager.setCurrentItem(0);
    }

    /**
     * 热门赛事
     */
    @Bind(R.id.rmssTab)
    JTabLayout rmssTab;
    @Bind(R.id.rmssPager)
    AutoViewPager rmssPager;
    ViewPagerAdapter rmssAdapter;
    private void initRmss(){
        rmssAdapter = new ViewPagerAdapter();
        rmssPager.setAdapter(rmssAdapter);
        rmssPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rmssTab.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        rmssTab.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(@NonNull Tab var1) {
                rmssPager.setCurrentItem(var1.getPosition());
            }

            @Override
            public void onTabUnselected(@NonNull Tab var1) {

            }

            @Override
            public void onTabReselected(@NonNull Tab var1) {

            }
        });
        initRmssData();
    }
    private String[] rmss = {"足球","篮球"};
    private Object[][] rmssList = {
            {"土超","02-20 18:30","利物浦","切尔西",43},
            {"土超","02-20 18:30","利物浦","切尔西",32},
            {"土超","02-20 18:30","利物浦","切尔西",64},
            {"土超","02-20 18:30","利物浦","切尔西",34},
            {"土超","02-20 18:30","利物浦","切尔西",63},
            {"土超","02-20 18:30","利物浦","切尔西",74},
            {"土超","02-20 18:30","利物浦","切尔西",745},
            {"土超","02-20 18:30","利物浦","切尔西",85},
            {"土超","02-20 18:30","利物浦","切尔西",576},
            {"土超","02-20 18:30","利物浦","切尔西",45},
    };
    private List<Rmss> getRmss(){
        List<Rmss> rmsses = new ArrayList<>();
        for(Object[] o:rmssList){
            Rmss rmss = new Rmss();
            rmss.setTag((String)o[0]);
            rmss.setTime((String)o[1]);
            rmss.setName1((String)o[2]);
            rmss.setName2((String)o[3]);
            rmss.setFanan((int)o[4]);
            rmsses.add(rmss);
        }
        return rmsses;
    }
    private void initRmssData(){
        List<View> views = new ArrayList<>();
        for(String s:rmss){
            Tab tab = rmssTab.newTab().setTitle(s);
            tab.setTitleColor(getResources().getColor(R.color.tabDefaultColor)
                    ,getResources().getColor(R.color.white));
            rmssTab.addTab(tab);
            View tView =  LayoutInflater.from(mContext).inflate(R.layout.pager_recycleview, null);
            RecyclerView rv = tView.findViewById(R.id.rv);
            rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            rv.setHasFixedSize(true);
            RmssAdapter rmssAdapter = new RmssAdapter(R.layout.item_tuijian_rmss,getRmss());
            rmssAdapter.setHasStableIds(true);
            rv.setAdapter(rmssAdapter);
            views.add(tView);
        }
        rmssAdapter.update(views);
        rmssPager.setCurrentItem(0);
    }

    /**
     * 新闻
     */
    @Bind(R.id.zxzxRv)
    AutoRecyclerView zxzxRv;
    private ZxzxAdapter zxzxAdapter;
    private void initZxzx(){
        zxzxRv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        zxzxRv.setHasFixedSize(true);
        zxzxAdapter = new ZxzxAdapter(R.layout.item_tuijian_zxzx,getZxzx());
        zxzxAdapter.setHasStableIds(true);
        zxzxRv.setAdapter(zxzxAdapter);
    }

    private Object[][] zxzxList = {
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"},
            {R.drawable.icon_haibao,"技术的房间山豆根士大夫山豆根士大夫","时间：2021/01/23","NBA"}
    };
    private List<Zxzx> getZxzx(){
        List<Zxzx> rmsses = new ArrayList<>();
        for(Object[] o:zxzxList){
            Zxzx rmss = new Zxzx();
            rmss.setImg((int)o[0]);
            rmss.setTitle((String)o[1]);
            rmss.setTime((String)o[2]);
            rmss.setTag((String)o[3]);
            rmsses.add(rmss);
        }
        return rmsses;
    }


    /**
     * 爆料
     */
    @Bind(R.id.haoliaoTab)
    JTabLayout haoliaoTab;
    @Bind(R.id.haoliaoPager)
    AutoViewPager haoliaoPager;
    ViewPagerAdapter haoliaoAdapter;
    private void initHaoliao(){
        haoliaoAdapter = new ViewPagerAdapter();
        haoliaoPager.setAdapter(haoliaoAdapter);
        haoliaoPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                haoliaoTab.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        haoliaoTab.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(@NonNull Tab var1) {
                haoliaoPager.setCurrentItem(var1.getPosition());
            }

            @Override
            public void onTabUnselected(@NonNull Tab var1) {

            }

            @Override
            public void onTabReselected(@NonNull Tab var1) {

            }
        });
        initHaoliaoData();
    }
    private String[] haoliao = {"前线爆料","百家贴士","盘口分析"};
    private Object[][] haoliaoList = {
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"},
            {"会告诉对方不干涉对方的的公司的","02-20 18:30"}
    };
    private List<Gdhl> getHaoliao(){
        List<Gdhl> rmsses = new ArrayList<>();
        for(Object[] o:haoliaoList){
            Gdhl rmss = new Gdhl();
            rmss.setTitle((String)o[0]);
            rmss.setDate((String)o[1]);
            rmsses.add(rmss);
        }
        return rmsses;
    }
    private void initHaoliaoData(){
        List<View> views = new ArrayList<>();
        for(String s:haoliao){
            Tab tab = haoliaoTab.newTab().setTitle(s);
            tab.setTitleColor(getResources().getColor(R.color.tabHaoliao)
                    ,getResources().getColor(R.color.tabHaoliaoP));
            haoliaoTab.addTab(tab);
            View tView =  LayoutInflater.from(mContext).inflate(R.layout.pager_recycleview, null);
            RecyclerView rv = tView.findViewById(R.id.rv);
            rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            rv.setHasFixedSize(true);
            GdhlAdapter gdhlAdapter = new GdhlAdapter(R.layout.item_tuijian_gdhl,getHaoliao());
            gdhlAdapter.setHasStableIds(true);
            rv.setAdapter(gdhlAdapter);
            views.add(tView);
        }
        haoliaoAdapter.update(views);
        haoliaoPager.setCurrentItem(0);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.frm_tuijian;
    }

    private void init(){

    }
}
